package com.ruoyi.workflow.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.workflow.mapper.StoreInfoMapper;
import com.ruoyi.workflow.domain.StoreInfo;
import com.ruoyi.workflow.service.IStoreInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * storeinfoService业务层处理
 *
 * @author zj
 * @date 2022-11-24
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements IStoreInfoService {
    @Autowired
    private ISysUserService userService;

    /**
     * 查询storeinfo列表
     *
     * @param storeInfo storeinfo
     * @return storeinfo
     */
    @Override
    public List<StoreInfo> selectStoreInfoList(StoreInfo storeInfo) {
        return getBaseMapper().selectStoreInfoList(storeInfo);
    }

    /**
     * 添加
     *
     * @param entity
     * @return
     */
    @Override
    public boolean save(StoreInfo entity) {
        if (StoreInfo.DELETE_STATUS.equals(entity.getStatus())) {
            throw new ServiceException("不允许添加为删除状态");
        }
        SysUser user = userService.selectUserById(entity.getManagerId());
        //设置管理员姓名
        entity.setManagerName(user.getNickName());
        //设置管理员联系方式
        entity.setManagerTel(user.getPhonenumber());
        //设置创建人员id
        entity.setCreatorId(SecurityUtils.getUserId());
        //设置创建时间
        entity.setCreatedTime(new Date());
        return super.save(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateById(StoreInfo entity) {
        if (StoreInfo.DELETE_STATUS.equals(entity.getStatus())) {
            throw new ServiceException("不允许修改为删除状态");
        }
        SysUser user = userService.selectUserById(entity.getManagerId());
        //设置管理员姓名
        entity.setManagerName(user.getNickName());
        //设置管理员联系方式
        entity.setManagerTel(user.getPhonenumber());
        //设置修改人员id
        entity.setUpdatorId(SecurityUtils.getUserId());
        //设置修改时间
        entity.setUpdatedTime(new Date());
        return super.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeBatchByIds(Collection<?> list) {

        return update(new UpdateWrapper<StoreInfo>()
                .set("status", 2)
                .in("id", list));
    }
}
